/* 
 * File:   MyBatchUnitModel.cpp
 * Author: Morpheus
 * 
 * Created on 20 juin 2013, 23:40
 */

#include <QtCore/qvariant.h>
#include <QtCore/qfileinfo.h>
#include <QtGui/qfont.h>
#include <QtGui/qcolor.h>

#include "MyBatchUnitModel.h"
//#include <QtCore/qfileinfo.h>

MyBatchUnitModel::MyBatchUnitModel(QObject* parent)
 : QAbstractTableModel(parent) {
}

MyBatchUnitModel::~MyBatchUnitModel() {
}

MyBatchMerge MyBatchUnitModel::getBatchMerge() const {
    return m_data;
}

void MyBatchUnitModel::setBatchMerge(const MyBatchMerge& data) {
    emit layoutAboutToBeChanged();
    this->m_data = data;
    emit layoutChanged();
    setModified(false);
}

MyBatchUnit MyBatchUnitModel::getBatchUnit(const QModelIndex& index) const{
    if(index.isValid())
        return m_data.getBatchList().at(index.row());
    return MyBatchUnit();
}
void MyBatchUnitModel::insertBatchUnit(const int& i, const MyBatchUnit& v){
    emit layoutAboutToBeChanged();
    if(m_data.insertBatch(v, i)){
        setModified(true);
    }
    emit layoutChanged();
}
void MyBatchUnitModel::removeBatchUnit(const QModelIndex& index){
    if(!index.isValid())
        return;
    MyBatchUnit unit = m_data.getBatchList().at(index.row());
    
    emit layoutAboutToBeChanged();
    if(m_data.removeBatch(unit))
        setModified(true);
    emit layoutChanged();
}
bool MyBatchUnitModel::goUpBatchUnit(const QModelIndex& index){
    if(!index.isValid() || index.row() == 0)
        return false;
    emit layoutAboutToBeChanged();
    if(m_data.swapBatch(index.row(), index.row()-1)){
        setModified(true);
    }
    emit layoutChanged();
    return true;
}
bool MyBatchUnitModel::goDownBatchUnit(const QModelIndex& index){
    if(!index.isValid() || index.row() >= rowCount()-1)
        return false;
    emit layoutAboutToBeChanged();
    if(m_data.swapBatch(index.row(), index.row()+1))
        setModified(true);
    emit layoutChanged();
    return true;
}

QString MyBatchUnitModel::getCurrUnit() const{
    return m_currUnit;
}
void MyBatchUnitModel::setCurrUnit(const QString& v){
    emit layoutAboutToBeChanged();
    m_currUnit = v;
    emit layoutChanged();
}

bool MyBatchUnitModel::isModified() const{
    return m_modified;
}
void MyBatchUnitModel::setModified(const bool& b){
    if(m_modified != b){
        m_modified = b;
        emit modified(m_modified);
    }
    if(m_saveFile != m_data.getSaveFile()){
        m_saveFile = m_data.getSaveFile();
        emit saveFile(m_saveFile, m_data.isSaveFileOk());
    }
}
void MyBatchUnitModel::setDoWithSuccess(const bool& b){
    emit layoutAboutToBeChanged();
    m_data.setDoWithSuccess(b);
    emit layoutChanged();
}

void MyBatchUnitModel::setName(const QString& v){
    if(v != m_data.getName()){
        m_data.setName(v);
        setModified(true);
    }
}
void MyBatchUnitModel::setSaveFilePatern(const QString& v){
    if(v != m_data.getSaveFilePatern()){
        m_data.setSaveFilePatern(v);
        setModified(true);
    }
}
void MyBatchUnitModel::setSaveFileFormat(const QString& v){
    if(v != m_data.getSaveFilePatern()){
        m_data.setSaveFileFormat(v);
        setModified(true);
    }
}
int MyBatchUnitModel::rowCount(const QModelIndex &parent) const{
    if(parent.isValid())
        return 0;
    return m_data.getBatchList().size();
}
int MyBatchUnitModel::columnCount(const QModelIndex& /*parent*/) const{
    return 2;
}
QVariant MyBatchUnitModel::data(const QModelIndex &index, int role) const{
    if(!index.isValid())
        return QVariant();
    if(role == Qt::DisplayRole){
        MyBatchUnit unit = m_data.getBatchList().at(index.row());
        switch(index.column()){
            case 0:
                if(!unit.isFile()){
                    return trUtf8("Script%1").arg( unit.isModified()? "*" : "" );
                }else{
                    return trUtf8("Fichier%1").arg( unit.isModified()? "*" : "" );
                }
            break;
            case 1:
                if(!unit.isFile()){
                    return unit.getContentText();
                }else{
                    return unit.getFileInfo().fileName();
                }
            break;
        }
    }else if(role == Qt::ToolTipRole && index.column() == 1){
        if(m_data.getBatchList().at(index.row()).isFile()){
            return  (m_data.getBatchList().at(index.row()).getFilePath() + tr("\n") + m_data.getBatchList().at(index.row()).getFileInfo().absoluteFilePath());
            
        }   
    }else if(role == Qt::BackgroundRole && !m_currUnit.isEmpty()){
        if(m_data.getBatchList().at(index.row()).getUUID() == m_currUnit){
            return QColor("orange");
        }
    }else if(role == Qt::FontRole && !m_currUnit.isEmpty()){
        if(m_data.getBatchList().at(index.row()).getUUID() == m_currUnit){
            QFont font;         font.setBold(true);
            return font;
        }
    }
    return QVariant();
}
QVariant MyBatchUnitModel::headerData(int section, Qt::Orientation orientation, int role) const{
    if(role == Qt::DisplayRole && orientation == Qt::Horizontal){
        switch(section){
            case 0:
                return trUtf8("Type");
            case 1:
                return trUtf8("Description");
        }
    }
    
    return QAbstractTableModel::headerData(section, orientation, role);
}
